﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.DALFactory;

    public class SalesOrderDetailBLL
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private string currentWhName;
        private List<string> dataBaseID;
        private PubParasInfo pubParasInfo;
        private static readonly ISalesOrderDetailDA salesOrderDetailDA = DataAccess.CreateSalesOrderDetailDA();
        private static readonly ISkuDA skuDA = DataAccess.CreateSkuDA();

        public SalesOrderDetailBLL()
        {
            this.pubParasInfo = null;
            this.currentWhName = string.Empty;
        }

        public SalesOrderDetailBLL(string currentWH)
        {
            this.pubParasInfo = null;
            this.currentWhName = string.Empty;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.currentWhName = currentWH;
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public SalesOrderDetailBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            this.currentWhName = string.Empty;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.currentWhName = tempPubParasInfo.CurrentWH;
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string DeleteSalesOrderDetail(string orderIDs, List<string> LineIDs)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                int i;
                List<SalesOrderDetailInfo> salesOrderDetailInfos = new List<SalesOrderDetailInfo>();
                SalesOrderDetailInfo salesOrderDetailInfo = null;
                for (i = 0; i < LineIDs.Count; i++)
                {
                    salesOrderDetailInfo = salesOrderDetailDA.GetSalesOrderDetailByID(dataBase, tran, orderIDs, LineIDs[i], this.dataBaseID[0], this.pubParasInfo);
                    salesOrderDetailInfos.Add(salesOrderDetailInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (salesOrderDetailDA.DeleteSalesOrderDetail(dataBase, tran, orderIDs, LineIDs, this.dataBaseID[0]) > 0)
                {
                    for (i = 0; i < LineIDs.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<SalesOrderDetailInfo>(dataBase, tran, salesOrderDetailInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "SALES_ORDER_DETAIL", this.dataBaseID[0]);
                        string CS00002 = temp;
                        temp = CS00002 + orderIDs + " , " + LineIDs[i] + ";";
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            if (temp.Length > 0)
            {
                return temp.Substring(0, temp.Length - 1);
            }
            return "";
        }

        public SalesOrderDetailInfo GetSalesOrderDetailByID(string OrderID, string LineID)
        {
            return salesOrderDetailDA.GetSalesOrderDetailByID(OrderID, LineID, this.dataBaseID[0], this.pubParasInfo);
        }

        public string InsertSalesOrderDetail(SalesOrderDetailInfo salesOrderDetailInfo)
        {
            salesOrderDetailInfo.UpdatedBy = salesOrderDetailInfo.CreatedBy;
            salesOrderDetailInfo.CreatedDate = DateTime.Now;
            salesOrderDetailInfo.UpdatedDate = DateTime.Now;
            if (salesOrderDetailInfo.OwnerID.Contains(","))
            {
                SkuInfo skuInfo = commonMethod.GetSkuInfoByUserOwner(salesOrderDetailInfo.SkuID, this.pubParasInfo.UserID, this.dataBaseID[0], this.pubParasInfo);
                if ((skuInfo == null) && string.IsNullOrEmpty(skuInfo.OwnerID))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F7100_001", this.pubParasInfo.UserLanguage, new List<string> { salesOrderDetailInfo.SkuID });
                }
            }
            else if (!skuDA.CheckSkuIDUnique(salesOrderDetailInfo.OwnerID, salesOrderDetailInfo.SkuID, this.dataBaseID[0]))
            {
                PublicMethod.GetInstance().ShowAlertMessage("F7100_001", this.pubParasInfo.UserLanguage, new List<string> { salesOrderDetailInfo.SkuID });
            }
            InboundHelper inboundHelper = new InboundHelper();
            AuditDataBLL auditDataBLL = new AuditDataBLL();
            salesOrderDetailInfo.OriginalQty = inboundHelper.ExchangeUom(salesOrderDetailInfo.PackID, salesOrderDetailInfo.UomID, salesOrderDetailInfo.OriginalQty, "W", this.dataBaseID[0]);
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string lineID = string.Empty;
            try
            {
                if (salesOrderDetailDA.InsertSalesOrderDetail(dataBase, tran, salesOrderDetailInfo, this.dataBaseID[0]) > 0)
                {
                    auditDataBLL.InsertAuditData<SalesOrderDetailInfo>(dataBase, tran, null, salesOrderDetailInfo, AuditAction.Insert, this.pubParasInfo.UserID, "SALES_ORDER_DETAIL", this.dataBaseID[0]);
                    lineID = salesOrderDetailInfo.LineID.ToString();
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return lineID;
        }

        public string UpdateSalesOrderDetail(SalesOrderDetailInfo salesOrderDetailInfo)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string lineID = string.Empty;
            try
            {
                salesOrderDetailInfo.UpdatedBy = salesOrderDetailInfo.CreatedBy;
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                salesOrderDetailInfo.OriginalQty = new InboundHelper().ExchangeUom(salesOrderDetailInfo.PackID, salesOrderDetailInfo.UomID, salesOrderDetailInfo.OriginalQty, "W", this.dataBaseID[0]);
                SalesOrderDetailInfo oldSalesOrderDetailInfo = salesOrderDetailDA.GetSalesOrderDetailByID(dataBase, tran, salesOrderDetailInfo.OrderID, salesOrderDetailInfo.LineID.ToString(), this.dataBaseID[0], this.pubParasInfo);
                if (salesOrderDetailDA.UpdateSalesOrderDetail(dataBase, tran, salesOrderDetailInfo, this.dataBaseID[0]) > 0)
                {
                    auditDataBLL.InsertAuditData<SalesOrderDetailInfo>(dataBase, tran, oldSalesOrderDetailInfo, salesOrderDetailInfo, AuditAction.Update, this.pubParasInfo.UserID, "SALES_ORDER_DETAIL", this.dataBaseID[0]);
                    lineID = salesOrderDetailInfo.LineID.ToString();
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return lineID;
        }
    }
}

